<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="分类位置" prop="videoCategoryParentId">-->
<!--        <el-select v-model="queryParams.videoCategoryParentId" placeholder="请选择" clearable size="small">-->
<!--          <el-option label="请选择" value="" />-->
<!--          <el-option label="精选" value="0" />-->
<!--          <el-option label="标签" value="1" />-->


<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="视频分类名称" prop="videoCategoryName">
        <el-input
          v-model="queryParams.videoCategoryName"
          placeholder="请输入视频分类名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="分类状态 " prop="videoCategoryState">
        <el-select v-model="queryParams.videoCategoryState" placeholder="请选择" clearable size="small">
          <el-option label="请选择" value="" />
          <el-option label="展示" value="0" />
          <el-option label="未展示" value="1" />


        </el-select>
      </el-form-item>
      <el-form-item label="模式选择" prop="videoCategoryPattern">
        <el-select v-model="queryParams.videoCategoryPattern" placeholder="请选择" clearable size="small">
          <el-option label="请选择" value="" />
          <el-option label="模式1(6小)" value="0" />
          <el-option label="模式2(1大5小)" value="1" />
          <el-option label="模式3(综艺及演员)" value="2" />
          <el-option label="模式4(4小)" value="3" />
          <el-option label="模式5(漫画格式)" value="4" />

        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['xqsp:category:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['xqsp:category:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['xqsp:category:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['xqsp:category:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="categoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="视频分类id" align="center" prop="spVideoCategory.videoCategoryId" />
<!--      <el-table-column label="分类位置" align="center" prop="spVideoCategory.videoCategoryParentId">-->

<!--        <template slot-scope="scope">-->
<!--          <el-switch-->
<!--            v-model="scope.row.spVideoCategory.videoCategoryParentId"-->
<!--            active-color="#13ce66"-->
<!--            inactive-color="#ff4949"-->
<!--            :active-value=0-->
<!--            :inactive-value=1-->
<!--            active-text="精选"-->
<!--            inactive-text="标签"-->
<!--            @change="handleChange(scope.row)">-->
<!--          </el-switch>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="视频分类名称" align="center" prop="spVideoCategory.videoCategoryName" />
      <el-table-column label="分类排序" align="center" prop="spVideoCategory.videoCategoryWeight" />
      <el-table-column label="模式选择" align="center" prop="spVideoCategory.videoCategoryPattern" />
      <el-table-column label="分类状态 " align="center" prop="spVideoCategory.videoCategoryState" >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.spVideoCategory.videoCategoryState"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="0"
            :inactive-value="1"
            active-text="展示"
            inactive-text="不展示"
            @change="handleStatusChange(scope.row)">
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['xqsp:category:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['xqsp:category:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改分类子表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="视频分类名称" prop="videoCategoryName">
          <el-input v-model="form.videoCategoryName" placeholder="请输入视频分类名称" />
        </el-form-item>
        <el-form-item label="分类排序" prop="videoCategoryWeight">
          <el-input-number v-model="form.videoCategoryWeight" placeholder="请输入分类权重"  :min="0"/>
        </el-form-item>
        <el-form-item label="模式选择" prop="videoCategoryPattern" v-show="CategoryPattern !=2">
<!--          <el-input v-model="form.videoCategoryPattern" placeholder="请输入模式选择(0 模式1 ，1 模式2 )" />-->
          <el-radio-group v-model="form.videoCategoryPattern">
            <el-radio :label="0" >模式1(6小)</el-radio>
            <el-radio :label="1"  >模式2(1大5小)</el-radio>
            <el-radio :label="3"  >模式4(4小)</el-radio>
            <el-radio :label="4"  >模式5(漫画格式)</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCategory, getCategory, delCategory, addCategory, updateCategory, exportCategory,updateCategoryState ,updateCategoryParent } from "@/api/xqsp/category";
export default {
  name: "Category",
  components: {
  },
  data() {
    return {
      //父分类下拉框
      options:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分类子表表格数据
      categoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      CategoryPattern:"",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        videoCategoryParentId: null,
        videoCategoryName: null,
        videoCategoryWeight: null,
        videoCategoryState: null,
        videoCategoryPattern: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        videoCategoryName: [
          {required: true, message: "分类名字不能为空", trigger: "blur"}
        ],
        videoCategoryPattern: [
          {required: true, message: "模式选择不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();

  },
  methods: {


    /** 查询分类子表列表 */
    getList() {
      this.loading = true;
      listCategory(this.queryParams).then(response => {
        this.categoryList = response.rows;
        this.categoryList.forEach(item=>{
          if (item.spVideoCategory.videoCategoryPattern==0){
            item.spVideoCategory.videoCategoryPattern="模式1"
          }else if (item.spVideoCategory.videoCategoryPattern==1){
            item.spVideoCategory.videoCategoryPattern="模式2"
          }else if (item.spVideoCategory.videoCategoryPattern==2){
            item.spVideoCategory.videoCategoryPattern="模式3"
          }
          else if (item.spVideoCategory.videoCategoryPattern==3){
            item.spVideoCategory.videoCategoryPattern="模式4"
          }
          else if (item.spVideoCategory.videoCategoryPattern==4){
            item.spVideoCategory.videoCategoryPattern="模式5"
          }
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        videoCategoryId: null,
        videoCategoryParentId: null,
        videoCategoryName: null,
        videoCategoryWeight: null,
        videoCategoryState: null,
        videoCategoryPattern: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.videoCategoryId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加分类子表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();

        const videoCategoryId = row.spVideoCategory.videoCategoryId || this.ids
        getCategory(videoCategoryId).then(response => {
          this.form = response.data;
          this.CategoryPattern=parseInt(response.data.videoCategoryPattern);
          this.form.videoCategoryPattern=parseInt(response.data.videoCategoryPattern);
          this.open = true;
          this.title = "修改分类子表";
        });


    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.videoCategoryId != null) {
            updateCategory(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCategory(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const videoCategoryIds = row.spVideoCategory.videoCategoryId || this.ids;
      if (row.spVideoCategory.videoCategoryPattern=="模式3"){
        this.msgError("模式三不可删除");
      }
     else if (videoCategoryIds==9 ||videoCategoryIds==10 ||videoCategoryIds==11){
        this.msgError("无法删除");
      }

      else{
        this.$confirm('是否确认删除分类子表编号为"' + videoCategoryIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCategory(videoCategoryIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      }

    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有分类子表数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCategory(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    // 是否展示
    handleStatusChange(row) {
      let text = row.spVideoCategory.videoCategoryState === 0 ? "展示" : "不展示";
      this.$confirm('确认要"' + text + '""' + row.spVideoCategory.videoCategoryName + '"分类吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return updateCategoryState(row.spVideoCategory.videoCategoryId, row.spVideoCategory.videoCategoryState);
      }).then(() => {
        this.msgSuccess(text + "成功");
      }).catch(function() {
        row.spVideoCategory.videoCategoryState = row.spVideoCategory.videoCategoryState === 0 ? 1 : 0;
      });
    },
    // // 显示位置
    // handleChange(row) {
    //   let text = row.spVideoCategory.videoCategoryParentId === 0 ? "精选" : "标签";
    //   this.$confirm('确认要把"'+ row.spVideoCategory.videoCategoryName +  '"设为"'+ text +  '"吗?', "警告", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning"
    //   }).then(function() {
    //     return updateCategoryParent(row.spVideoCategory.videoCategoryId, row.spVideoCategory.videoCategoryParentId);
    //   }).then(() => {
    //     this.msgSuccess(text + "成功");
    //   }).catch(function() {
    //     row.spVideoCategory.videoCategoryParentId = row.spVideoCategory.videoCategoryParentId === 0 ? 1 : 0;
    //   });
    // },
  }
};


</script>
<style>

</style>
